package com.it.controller;

import com.it.pojo.Result;
import com.it.pojo.User;
import com.it.service.UserService;
import com.it.utils.JwtUtil;
import com.it.utils.Md5Util;
import com.it.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result register(String username, String password) {
        // 使用 Md5Util 对密码进行加密
        String Md5Password = Md5Util.getMD5String(password);

        User u = userService.findByUserName(username);
        if (u == null) {
            // 注册时存储加密后的密码
            userService.register(username, Md5Password);
            return Result.success();
        } else {
            return Result.error("用户名已经被占用");
        }
    }

    @PostMapping("/login")
    public Result<String> login(String username, String password) {
        User loginUser = userService.findByUserName(username);
        if (loginUser == null) {
            return Result.error("用户名错误");
        }

        // 使用 Md5Util 对输入的密码进行加密，然后与数据库中存储的加密密码进行比对
        String Md5Password = Md5Util.getMD5String(password);
        if (loginUser.getPassword().equals(Md5Password)) {
            // 登录成功
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", loginUser.getId());
            claims.put("username", loginUser.getUsername());
            String token = JwtUtil.genToken(claims);
            return Result.success(token);
        }
        return Result.error("密码错误");
    }

    @GetMapping("/userInfo")
    public Result<User> userInfo() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = userService.findByUserName(username);
        return Result.success(user);
    }

    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        userService.update(user);
        return Result.success("修改成功");
    }

    @PatchMapping("/updateAvatar")
    public Result updateAvatar(@RequestParam String avatarUrl) {
        userService.updateAvatar(avatarUrl);
        return Result.success("头像更新成功！");
    }

    @PatchMapping("/updatePwd")
    public Result updatePwd(@RequestBody Map<String, String> params) {
        // 1. 校验参数
        String oldPwd = params.get("old_pwd");
        String newPwd = params.get("new_pwd");
        String rePwd = params.get("re_pwd");

        if (!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)) {
            return Result.error("缺少必要的参数");
        }

        // 2. 原密码是否正确
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User loginUser = userService.findByUserName(username);

        // 使用 Md5Util 对旧密码进行加密后比对
        String Md5Password = Md5Util.getMD5String(oldPwd);
        if (!loginUser.getPassword().equals(Md5Password)) {
            return Result.error("原密码填写不正确");
        }

        // 3. newPwd 和 rePwd 是否一致
        if (!newPwd.equals(rePwd)) {
            return Result.error("两次填写的新密码不一样");
        }

        // 4. 使用 Md5Util 对新密码进行加密后更新
        String Md5PasswordNew = Md5Util.getMD5String(newPwd);
        userService.updatePwd(Md5PasswordNew);
        return Result.success("密码修改成功");
    }
}